/*
 *    Copyright (c) 2018-2025, workdata All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: workdata
 */
package com.workcell.workdata.xthings.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.entity.BaseLevelGauge;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeLevelGauge;
import com.workcell.workdata.xthings.dto.CodeChainnumVo;
import com.workcell.workdata.xthings.entity.*;
import com.workcell.workdata.xthings.mapper.CodeLevelGaugeMapper;
import com.workcell.workdata.xthings.service.BaseLevelGaugeService;
import com.workcell.workdata.xthings.service.CodeLevelGaugeService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 码库液位计信息表
 *
 * @author pigx code generator
 * @date 2023-05-16 13:58:21
 */
@Service
public class CodeLevelGaugeServiceImpl extends ServiceImpl<CodeLevelGaugeMapper, CodeLevelGauge> implements CodeLevelGaugeService {

    @Resource
    private BaseLevelGaugeService baseLevelGaugeService;

    /**
     * 保存液位计二维码基本快照信息
     * @param codeChainnum
     * @return
     */
    public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum, CodeChainnumVo.SaveChainNumVo saveChainNumVo) {
        // 查询硬件信息
        LambdaQueryWrapper<BaseLevelGauge> baseLevelGaugeCondition = new QueryWrapper<BaseLevelGauge>().lambda()
                .eq(BaseLevelGauge::getId,codeChainnum.getBaseId());
        BaseLevelGauge baseLevelGauge = baseLevelGaugeService.getOne(baseLevelGaugeCondition);
        if (ObjectUtil.isEmpty(baseLevelGauge)){
            return false;
        }
        // 添加二维码快照信息
        CodeLevelGauge saveBean = new CodeLevelGauge();
        BeanUtil.copyProperties(baseLevelGauge,saveBean, "createBy","createTime","updateBy","updateTime");
        saveBean.setCodeId(codeChainnum.getCodeId());
        saveBean.setChainNum(codeChainnum.getChainNum());
        saveBean.setDeviceNo(codeChainnum.getDeviceNo());
        saveBean.setDiameter(saveChainNumVo.getDiameter());
        saveBean.setTotalVolume(saveChainNumVo.getTotalVolume());
        return this.save(saveBean);
    }

    /**
     * 获取硬件清单
     */
    public List<JSONObject> getHardwareList() {
		LambdaQueryWrapper<BaseLevelGauge> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseLevelGauge.class);
		lambdaQueryWrapper.eq(BaseLevelGauge::getState, HardwareStateEnum.INUSE.getCode());
        return baseLevelGaugeService.list(lambdaQueryWrapper).stream().map(i -> {
            JSONObject resultBean = new JSONObject();
            resultBean.put("baseId",i.getId());
            resultBean.put("manufacturer",i.getManufacturer());
            resultBean.put("hardwareName",i.getName());
            return resultBean;
        }).collect(Collectors.toList());
    }


    /**
     * 查询硬件基本信息
     * @param baseIds
     * @return
     */
    public Object getHardwareBase(String baseIds) {
        List<String> baseList = Arrays.asList(baseIds.split(","));
        LambdaQueryWrapper<BaseLevelGauge> baseLevelGaugeCondition = new QueryWrapper<BaseLevelGauge>().lambda()
                .in(CollectionUtil.isNotEmpty(baseList),BaseLevelGauge::getId,baseList);
        List<BaseLevelGauge> baseLevelGaugeList = baseLevelGaugeService.list(baseLevelGaugeCondition);
        return baseLevelGaugeList;
    }


    /**
     * 查询硬件快照信息
     */
    public Object getHardwareInfo(String codeId) {
        return this.getById(codeId);
    }


    @Override
    public CodeLevelGauge getCodeLevelGaugeInfo(String codeId) {
        return this.getById(codeId);
    }

    @Override
    public CodeLevelGauge getLevelGaugeModel(String deviceNo) {
        Assert.isTrue(ObjectUtil.isNotEmpty(deviceNo),"设备编码不能为空");
        LambdaQueryWrapper<CodeLevelGauge> baseLevelGaugeCondition = new QueryWrapper<CodeLevelGauge>().lambda()
                .eq(CodeLevelGauge::getDeviceNo,deviceNo);
        CodeLevelGauge codeLevelGauge = baseMapper.selectOne(baseLevelGaugeCondition);
        return codeLevelGauge;
    }
}
